package com.example.sql.manager.ftl;

import com.example.sql.manager.ftl.directive.ArrAddDirective;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;

import java.io.Serializable;
import java.nio.charset.StandardCharsets;

public class FtlConfigSingleton implements Serializable {

    private static final Configuration SINGLETON;

    static {
        SINGLETON = new Configuration(Configuration.VERSION_2_3_31);
        SINGLETON.setDefaultEncoding(StandardCharsets.UTF_8.name());
        SINGLETON.setTemplateLoader(new StringTemplateLoader());
        //自定义指令
        SINGLETON.setSharedVariable("arr_add", new ArrAddDirective());
    }

    private FtlConfigSingleton() {
    }

    public static Configuration getSingleton() {
        return SINGLETON;
    }

    /**
     * ★★★ 增加了 readResolve() 方法以后，就可以保证序列化的情况下也能够实现单例模式
     */
    private Object readResolve() {
        return SINGLETON;
    }
}
